const api = require("../../../ws_micro_shop/utils/api.js");
const app = getApp();
const {
  gatherAction = "",
  reportAction = ""
} = require("../../../ws_micro_shop/utils/utils");

let is_loading_more = false;
let is_no_more = false;

Page({
  data: {
    cat_id: "",
    page: 1,
    goods_list: [],
    sort: 0,
    sort_type: -1,
    abnorData: {
      imgSrc: "/common/static/images/abnor-08.png",
      text: "抱歉！没找到相关商品~"
    },
    theme: app.themeData,
    unit: "",
    curr: "",
    curr2: "",
    level2: "",
    typelist: [],
    typesublist: [],
    showsubtype: false,
    p: 2,
    height: "",
    heightTop: "",
    _switch: false,
    checked: true,
    isTabShow: true, //二级页是否全部展示
    toView: ""
  },
  onLoad(options) {
    let that = this;
    let opts = options || {};
    app.v8_card_seller_actions_log = [];
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit
      });
    });
    this.setData({
      opts
    });
    this.getPosterType();
    this.getPosterLists(this.data.curr);
    this.setData({
      height: wx.getSystemInfoSync().windowHeight - 45
    });
    // this.loadData(options);
  },
  onShow() {},

  onHide() {
    reportAction && reportAction();
  },

  onUnload() {
    reportAction && reportAction();
  },

  loadData(options) {
    let _this = this;
    _this.reloadGoodsList();
  },
  // onReachBottom() {
  //   if (is_no_more) {
  //     return;
  //   }
  //   this.loadMoreGoodsList();
  // },
  collectFormId(e) {
    app.collectFormId(e);
  },

  gotoGoodsDetail(e) {
    gatherAction &&
      gatherAction("gotoGoodsDetail", "success", {
        goodsID: e.currentTarget.dataset.tmpid
      });
  },

  onSearchGoods() {
    let { keyword } = this.data;
    keyword ||
      wx.showToast({
        title: "请输入关键词",
        icon: "none"
      });
    this.setData({
      curr: "",
      curr2: "",
      p: 2,
      _switch: true,
      list: [],
      toView: "",
      typesublist: []
    });
    keyword && this.getPosterLists();
  },

  onKeywordInput(e) {
    this.setData({ keyword: e.detail.value.trim() });
  },
  showAllTab() {
    this.setData({
      isTabShow: false
    });
  },
  hideAllTab() {
    this.setData({
      isTabShow: true
    });
  },
  getPosterType() {
    let _this = this;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.default.cat_list,
      data: {
        shop_id: 0
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let { list } = res.data;
          _this.setData({
            typelist: list
          });
        }
      }
    });
  },
  getPosterLists(id, subid = "") {
    let _this = this;
    let cat_id = _this.data.keyword ? "" : subid ? subid : id;
    wx.showLoading({
      title: "正在加载...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.default.goods_list,
      data: {
        cat_id: cat_id,
        is_integral: 0,
        shop_id: 0,
        page: 1,
        sort: _this.data.sort,
        sort_type: _this.data.sort_type,
        keyword: _this.data.keyword || ""
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          if (res.data.list) {
            _this.setData({
              list: res.data.list
            });
          }
        }
      }
    });
  },
  loadmore() {
    let that = this;
    let oldlist = that.data.list;
    let page = that.data.p;
    let cat_id = that.data.keyword
      ? ""
      : that.data.curr2
      ? that.data.curr2
      : that.data.curr;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.default.goods_list,
      data: {
        cat_id: cat_id,
        is_integral: 0,
        shop_id: 0,
        page: page,
        sort: that.data.sort,
        sort_type: that.data.sort_type,
        keyword: that.data.keyword || ""
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let list = res.data.list;
          let newlist = [];
          if (list.length > 0) {
            newlist = oldlist.concat(list);
            page += 1;
          } else {
            newlist = oldlist;
            // wx.showToast({
            //   title: '没有更多数据了',
            //   icon: 'none',
            // });
          }
          that.setData({
            list: newlist,
            p: page
          });
        }
      },
      complete: function() {}
    });
  },
  switchType(e) {
    let that = this;
    this.setData({ keyword: "" });
    let id = Number(e.currentTarget.dataset.typeid);
    let mainid = that.data.curr;
    let typelist = that.data.typelist;
    let subid = that.data.curr2;
    if (!subid) {
      subid = "";
    }
    this.setData({
      isTabShow: true
    });
    if (Number(mainid) !== Number(id)) {
      that.setData({
        curr: id,
        curr2: "",
        p: 2,
        _switch: true,
        list: [],
        toView: ""
      });
      that.getPosterLists(id);
    }
    if (id) {
      for (let i in typelist) {
        if (Number(typelist[i].id) === id) {
          let children_list = typelist[i].list || [];
          that.setData({
            typesublist: children_list
          });
          if (children_list.length > 0) {
            that.setData({
              showsubtype: true
            });
          } else {
            that.setData({
              showsubtype: false
            });
          }
          break;
        }
      }
    } else {
      that.setData({
        typesublist: [],
        showsubtype: false
      });
    }
  },
  chooseCateType(e) {
    let subid = Number(e.currentTarget.dataset.typetwoid);
    this.getPosterLists(this.data.curr, subid);
    this.setData({
      showsubtype: false,
      curr2: subid,
      _switch: true,
      list: [],
      isTabShow: true,
      p: 2
    });
    if (!this.data.isTabShow) {
      this.setData({
        isTabShow: true
      });
    }
    let target = e.currentTarget.dataset.opt;
    this.setData({
      toView: target
    });
  }
});
